<?php
namespace BoxEdu\Logic;
class AskLogic extends \HomeController {
	public function _initialize() {
		parent::_initialize();

	}

	/****提问列表***/
	public function get($where, $limit, $orderBy) {
		$list = BoxModel("SnsTopic")->get("ask", $where, $limit, $orderBy);
		return $list;
	}

	/****采纳回答***/
	public function accept($comment_id) {
		$data = BoxModel("SnsComment")->where(array('id' => $comment_id))->find();
		$topic_id = $data['pid'];
		$data['pidData'] = BoxModel("SnsTopic")->load($topic_id);
		if ($data['pidData']['user_id'] != $this->user_id) {
			return array(
				'error' => 1,
				'msg' => '您没有权限操作',
			);
		}
		//if ($data['user_id'] == $this->user_id) {
		//            return array(
		//                'error' => 3,
		//                'msg' => '不能采纳自己的回答',
		//            );
		//        }

		$user = BoxModel("edu_user")->load($data['user_id']);
		$userSubjectids = (array) json_decode($user['subjectids'], true);
		if (!$user['school_num']) {
			return array(
				'error' => 5,
				'msg' => '不能采纳非编程训练营学员的答案',
			);
		}
		$subjectid = $data['pidData']['pid'];
		$subjectInfo = BoxModel("edu_subject")->rootSubject($subjectid);
		$subjectid = $subjectInfo['subjectid'];
		//dump($userSubjectids);exit();
		if ($subjectid && (!$userSubjectids[$subjectid] || $userSubjectids[$subjectid]['end_date'] < date("Y-m-d"))) {
			return array(
				'error' => 6,
				'msg' => '回答者已毕业，或没开通课程，不能采纳当前答案',
			);
		}
		if ($data['pidData']['is_best'] == 1) {
			return array(
				'error' => 2,
				'msg' => '已采纳答案',
			);
		}
		$type = $data['type'];
		$r = BoxModel("SnsTopic")->best($data['pid']);
		$r2 = BoxModel("SnsComment")->best($type, $comment_id);
		if ($r !== false && $r2 !== false) {
			/******回答被采纳获得学币***/
			$setting = dict("setting");
			$num = $setting['accept_ask'];
			$row = array(
				'user_id' => $data['user_id'],
				'num' => $num,
				'url' => HomeUrl . U('Subject/topic_info', array('topic_id' => $data['pid'], 'comment_id' => $comment_id)),
				'title' => cutstr($data['pidData']['title'], 50),
				'info' => cutstr($data['content'], 400),
				'event' => "00004",
			);
			if ($num > 0) {
				$r = Logic('BoxSns', 'Log')->add($row);
			}
			/******提醒***/
			$params = array(
				'user_id' => $data['user_id'],
				'url' => HomeUrl . U('Subject/topic_info', array('topic_id' => $data['pid'])),
				'title' => cutstr($data['pidData']['title'], 50),
				'num' => $num,
			);
			Logic("BoxSns", "Noti")->send("00003", $params);

			return array(
				'error' => 0,
				'msg' => '已采纳答案',
			);
		}

	}

}